package cn.nexd.app.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import cn.nexd.app.arsenal.R;
import cn.nexd.app.util.AppUtils;

/**
 * Created by xun on 2016/5/5.
 * 沉浸式状态栏
 */
public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getLayoutResId());
        setStatusColor();
    }

    abstract protected int getLayoutResId();

    protected FrameLayout getContentLayout() {
        ViewGroup contentLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        if ((contentLayout instanceof FrameLayout)) {
            return (FrameLayout) contentLayout;
        }
        return null;
    }


    public void setSystemBarColor(int color) {
        Window window = getWindow();
        FrameLayout contentLayout = getContentLayout();
        if (contentLayout != null) {
            contentLayout.getChildAt(0).setFitsSystemWindows(true);
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                WindowManager.LayoutParams contentLayoutParams = new WindowManager.LayoutParams();
                contentLayoutParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR;
                window.setAttributes(contentLayoutParams);
            } else {
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                    window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    int statusBarHeight = AppUtils.getSystemBarHeight(this);
                    contentLayout.getChildAt(0).setPadding(0, statusBarHeight, 0, 0);
                }
            }
            SystemBarTintManager systemBarTint = new SystemBarTintManager(this);
            systemBarTint.setStatusBarTintEnabled(true);
            systemBarTint.setStatusBarTintColor(color);
        }

    }

    public void setStatusColor() {
        setSystemBarColor(getResources().getColor(R.color.colorPrimary));
    }



}

